#include "reverse.h"
#include "lib_string.h"

void reverse_io_config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	RCC_APB2PeriphClockCmd(REVERSE_RCC, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = REVERSE_GPIO_PIN; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(REVERSE_GPIO_PORT, &GPIO_InitStructure);
	
	EXTI_ClearITPendingBit(EXTI_Line4);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource4);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line4;

	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; 

	EXTI_InitStructure.EXTI_LineCmd = ENABLE;                                          

	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);                         

	NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;    

	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 

	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        

	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                  

	NVIC_Init(&NVIC_InitStructure);
}

static volatile unsigned int reverse_count = 0;
void reverse_io_irq(void)
{
	if(EXTI_GetITStatus(EXTI_Line4) != RESET) 
	{ 
		reverse_count++;
		if(reverse_count >= 0xFFFFFFFF) reverse_count = 0;
		raw_printf("Reverse_Cnt = %d\r\n", reverse_count);
		EXTI_ClearITPendingBit(EXTI_Line4);
	}  
}

